﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Utility;
using LotteryVote.Logic;
using LotteryVote.Entity;
using LotteryVote.Logic.Web;

namespace LotteryVote.Controls
{
    public partial class OrderSearchBar : BaseControl
    {
        #region Properties
        public string Num
        {
            get
            {
                return txt_num.Text;
            }
            set
            {
                txt_num.Text = value;
            }
        }
        public int CompanySelectedValue
        {
            get
            {
                return lv_company.SelectedValue;
            }
        }
        public int GamePlayWaySelectedValue
        {
            get
            {
                return lv_gameplayway.GamePlayWayId;
            }
        }
        public string SortField
        {
            get
            {
                return ddl_sortField.SelectedValue;
            }
        }
        public string SortType
        {
            get
            {
                return ddl_sortDesc.SelectedValue;
            }
        }
        public bool IsSearched
        {
            get
            {
                return !string.IsNullOrEmpty(Request["Search"]) && Request["Search"] == "true";
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnInit(EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSortField();
                InitSearchBar();
            }
            base.OnInit(e);
        }
        void InitSearchBar()
        {
            if (!IsSearched) return;
            Execute(() =>
            {
                if (!string.IsNullOrEmpty(Request["num"]))
                    txt_num.Text = Request["num"];
                int companyId;
                if (!string.IsNullOrEmpty(Request["company"]) && int.TryParse(Request["company"], out companyId))
                    lv_company.SelectedValue = companyId;
                int gameplaywayId;
                if (!string.IsNullOrEmpty(Request["gameplayway"]) && int.TryParse(Request["gameplayway"], out gameplaywayId))
                    lv_gameplayway.GamePlayWayId = gameplaywayId;
                if (!string.IsNullOrEmpty(Request["sortfield"]))
                    ddl_sortField.SelectedValue = Request["sortfield"];
                if (!string.IsNullOrEmpty("sorttype"))
                    ddl_sortDesc.SelectedValue = Request["sorttype"];
            });
        }
        void BindSortField()
        {
            Dictionary<string, string> fieldSource = new Dictionary<string, string>();
            fieldSource.Add(BetOrder.AMOUNT, GetGlobalResourceObject("Resource", BetOrder.AMOUNT).ToString());
            fieldSource.Add(BetOrder.TURNOVER, GetGlobalResourceObject("Resource", BetOrder.TURNOVER).ToString());
            fieldSource.Add(BetOrder.ODDS, GetGlobalResourceObject("Resource", BetOrder.ODDS).ToString());
            fieldSource.Add(BetOrder.COMMISSION, GetGlobalResourceObject("Resource", BetOrder.COMMISSION).ToString());
            fieldSource.Add(BetOrder.NET, GetGlobalResourceObject("Resource", BetOrder.NET).ToString());
            fieldSource.Add(BetOrder.NETAMOUNT, GetGlobalResourceObject("Resource", BetOrder.NETAMOUNT).ToString());
            fieldSource.Add(BetOrder.DROPWATER, GetGlobalResourceObject("Resource", BetOrder.DROPWATER).ToString());
            ddl_sortField.DataSource = fieldSource;
            ddl_sortField.DataTextField = "Value";
            ddl_sortField.DataValueField = "Key";
            ddl_sortField.DataBind();
        }

    }
}